package com.lbl.third.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbl.third.common.CustomException;
import com.lbl.third.dto.SetmealDto;
import com.lbl.third.enytity.Setmeal;
import com.lbl.third.enytity.SetmealDish;
import com.lbl.third.mapper.SetmealMapper;
import com.lbl.third.service.SetmealDishService;
import com.lbl.third.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;




import java.util.List;
import java.util.stream.Collectors;


@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;


    /**
     * 新增套餐，同时保存套餐和菜品的关系
     * @param setmealDto
     */
    @Transactional                 //同时操作两个表，开启事务
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);

        //获取套餐菜品信息
        List<SetmealDish> list = setmealDto.getSetmealDishes();

       list.stream().map((item) -> {
           //获取刚保存的套餐ID并保存在套餐菜品信息中
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

       //保存套餐菜品信息
             setmealDishService.saveBatch(list);
    }

    /**
     *删除套餐的同时删除套餐和菜品关联数据
     * @param ids
     */
    @Override
    public void removeWithDish(List<Long> ids) {
        //条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        //添加过滤条件
        queryWrapper.eq(Setmeal::getStatus,1);
        queryWrapper.in(Setmeal::getId,ids);
        int count=this.count(queryWrapper);

        if(count>0){
            throw new CustomException("有套餐正在售卖中，无法删除");
        }
        //通过ids删除套餐
        this.removeByIds(ids);
        //条件构造器
        LambdaQueryWrapper<SetmealDish> queryWrapper1=new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper1.in(SetmealDish::getSetmealId,ids);
        //根据过滤条件进行删除
        setmealDishService.remove(queryWrapper1);

    }

    /**
     * 修改套餐信息
     * @param setmealDto
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {

        //更新套餐基本信息
        this.updateById(setmealDto);
        //条件构造器
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());

        //setMealDish表根据套餐id删除相应的菜品
        setmealDishService.remove(queryWrapper);

        //获取更新后的套餐下的菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        //对更新后的菜品信息添加套餐id
        List<SetmealDish> list = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;

        }).collect(Collectors.toList());

        //将新增的菜品信息保存
        setmealDishService.saveBatch(list);
    }


}
